const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    hidden: true, //隐藏表单控件
    page: 1, //当前请求数据是第几页
    pageSize: 10, //每页数据条数
    hasMoreData: true, //上拉时是否继续请求数据，即是否还有更多数据
    contentlist: null,
    contentlistfa: null,
    defaultimg: "../../images/default.png",
    index: 0,
    classid: null,
    classlist: null
  },
  btnclick: function (e) {
    var index = e.currentTarget.dataset.index;
    // app.globalData.url = this.data.contentlist[index].htmlurl
    // wx.navigateTo({
    //   url: '../notifydetail/notifydetail'
    // })
    var contentid=this.data.contentlist[index].ContentID
    var contenttype=this.data.contentlist[index].ContentType
    var posterType=this.data.contentlist[index].PosterType
    wx.navigateTo({
      url: '../notifydetail/notifydetail?contentid='+contentid+"&contenttype="+contenttype+"&posterType="+posterType
    })
  },
  btnclick1: function (e) {
    var index = e.currentTarget.dataset.index;
    // app.globalData.url = this.data.contentlistfa[index].htmlurl
    // wx.navigateTo({
    //   url: '../notifydetail/notifydetail'
    // })
    var contentid=this.data.contentlistfa[index].ContentID
    var contenttype=this.data.contentlistfa[index].ContentType
    var posterType=this.data.contentlistfa[index].PosterType
    wx.navigateTo({
      url: '../notifydetail/notifydetail?contentid='+contentid+"&contenttype="+contenttype+"&posterType="+posterType
    })
  },
  onChange(event) {
    console.log(event.detail.index)
    this.setData({
      index: event.detail.index,
      hasMoreData: true,
      page: 1,
      contentlist: null,
      contentlistfa: null
    })
    if (event.detail.index == 0) {
      var that = this
      that.getInfo('正在加载数据...')
    } else {
      var that = this
      that.getInfofa('正在加载数据...')
    }

  },
  // 获取收件箱数据
  getInfo: function (message) {
    var that = this;
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: message,
    })
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "contentboxType": 2,
      "pagenum": that.data.page,
      "pagesize": that.data.pageSize,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileType": app.globalData.mobileType,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_notify"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.hideNavigationBarLoading() //在当前页面隐藏导航条加载动画
          wx.hideLoading() //隐藏 loading 提示框
          var contentlistTem = that.data.contentlist;
          if (res.data.data.length > 0) {
            if (that.data.page == 1) {
              contentlistTem = []
            }
            var contentlist = res.data.data;
            contentlist.forEach(function (item, index) {
              console.log(item); //这里的item就是从数组里拿出来的每一个每一组
              try {
                contentlist[index].Title = decodeURIComponent(item.Title)
              } catch (error) {
                // 此处是负责例外处理的语句
                contentlist[index].Title = item.Title
              } finally {
                // 此处是出口语句
              }
            })
            if (contentlist.length < that.data.pageSize) {
              that.setData({
                contentlist: contentlistTem.concat(contentlist),
                hasMoreData: false
              })
            } else {
              that.setData({
                contentlist: contentlistTem.concat(contentlist),
                hasMoreData: true,
                page: that.data.page + 1
              })
            }
          }
        } else {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      },
      complete: function (res) {
        console.log('submit complete');
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      }
    })
  },



  // 获取发件箱数据
  getInfofa: function (message) {
    var that = this;
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: message,
    })
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "contentboxType": 1,
      "pagenum": that.data.page,
      "pagesize": that.data.pageSize,
      "mobileType": app.globalData.mobileType,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_notify"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          var contentlistTem = that.data.contentlistfa;
          if (res.data.data.length > 0) {
            if (that.data.page == 1) {
              contentlistTem = []
            }
            var contentlist = res.data.data;
            contentlist.forEach(function (item, index) {
              console.log(item); //这里的item就是从数组里拿出来的每一个每一组

              try {
                contentlist[index].Title = decodeURIComponent(item.Title)
              } catch (error) {
                // 此处是负责例外处理的语句
                contentlist[index].Title = item.Title
              } finally {
                // 此处是出口语句
              }
            })
            if (contentlist.length < that.data.pageSize) {
              that.setData({
                contentlistfa: contentlistTem.concat(contentlist),
                hasMoreData: false
              })
            } else {
              that.setData({
                contentlistfa: contentlistTem.concat(contentlist),
                hasMoreData: true,
                page: that.data.page + 1
              })
            }
          }
        } else {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      },
      complete: function (res) {
        console.log('submit complete');
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    var list = wx.getStorageSync('classlist');
    var classlist = []
    var classobj = {
      text: "全园校园通知",
      value: 0
    }
    classlist.push(classobj)
    for (var i = 0; i < list.length; i++) {
      var classobj = {
        text: list[i].className+"的校园通知",
        value: list[i].classID
      }
      classlist.push(classobj)
    }
    this.setData({
      classid: classlist[0].value,
      classlist: classlist
    })
    //that.getInfo('正在加载数据...')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hasMoreData: true,
      page: 1,
      contentlist: null,
      contentlistfa: null
    })
    if (this.data.index == 0) {
      var that = this
      that.getInfo('正在加载数据...')
    } else {
      var that = this
      that.getInfofa('正在加载数据...')
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      contentlist: null,
      contentlistfa: null,
      hasMoreData: true,
      page: 1
    })
    if (this.data.index == 0) {
      this.getInfo('正在刷新数据')
    } else {
      this.getInfofa('正在刷新数据')
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.index == 0) {
      if (this.data.hasMoreData) {
        this.getInfo('加载更多数据')
      } else {
        wx.showToast({
          title: '没有更多数据',
        })
      }
    } else {
      if (this.data.hasMoreData) {
        this.getInfofa('加载更多数据')
      } else {
        wx.showToast({
          title: '没有更多数据',
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addnotify: function () {
    wx.navigateTo({
      url: '../addnotify/addnotify',
    })
  },

  onclasschange({
    detail
  }) {
    this.setData({
      classid: detail
    });
    this.setData({
      contentlist: null,
      contentlistfa: null,
      hasMoreData: true,
      page: 1
    })
    // this.getInfo('正在刷新数据')
    if (this.data.index == 0) {
      this.getInfo('正在刷新数据')
    } else {
      this.getInfofa('正在刷新数据')
    }
  },
})